
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gene.core;

//import gene.core.util.Surface;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
//import javax.microedition.lcdui.Graphics;
//import javax.microedition.lcdui.Image;

/**
 *
 * @author callagun
 */
public class GraphicDevice {

    //Graphics g = Core.getInstance().getCoreGraphics();

    int [] memory;

    
    public GraphicDevice(int w,int h){
        memory = new int[w * h];
    }


public void clearScreen(){


}

public void flush(){

 //Graphics Core.getInstance() = coreInstance.getCoreGraphics();

    int j = memory.length;
    //Image tmp = Image.createRGBImage(memory,Core.getInstance().W , Core.getInstance().H, true);
	
    //Core.graphics.drawImage(tmp,0,0,0);
}

public void bufferImage(Image img,int x, int y, int anchor){

}

/////////////////////////// sessao graficos /////////////////////////////
    //bufferiza no segundo passo;
    public void buffer(Component compo) {
        compo.draw();
    }

    public void draw_Pixel(int col, int row, int r,int g, int b) {
        // 00 00 00 00
        //int rr = color<<0;
        //int gg = 0;
        //int bb = color<<6;
        Core.graphics.setColor(new Color(r, g, b,0));
        Core.graphics.drawLine(col, row, col+1, row+1);
    }
 int i=0;

 public void clear_Screen() {
       
        //i+=0x00040000;
        Core.graphics.setColor(Color.black);//0x00000000);
        //graphics.setColor(i);
        //int x = Core.panel.WIDTH/2 - Core.W/2;
        //int y = Core.panel. HEIGHT/2 - Core.H/2;

        Core.graphics.fillRect(0,0, Core.W, Core.H);
    }

    /**
     * Save images on buffer to show him on <code>paint()</code>
     * @param x postition X on device screen
     * @param y postition Y on device screen
     */
    public void buffer(Image img, int x, int y) {

     //if (img != null && (Core.GLOBAL_TICKS%Core.FRAME_SKIP)==0) {
            try {

            //if(Core.fullScreen){
               
             Core.graphics.drawImage(img,x,y, null);
            //}

            //else{//posiciona relativa

              //  Core.graphics.drawImage(img, x, 22 + y, null);
               //img = null;}
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        //}
        img = null;
      /*
        if (img != null && (Core.GLOBAL_TICKS % Core.FRAME_SKIP) == 0) {
            //se estiver setado para rotacionar , recria todas as imagens re-orientando-as
        /*    if (orientation > 0) {
                graphics.drawImage(Image.createImage(img, 0, 0, img.getWidth(), img.getHeight(), orientation), y, x, 0);
            } else {*
                Core.graphics.drawImage(img, x, y, 0);
            //}
        }
        img = null;*/
    }

    /**
     * clear the device screen and show all elements of current <code>Screen</code>
     *
    public void /*flush* paint() {
      if(Core.GLOBAL_TICKS % Core.FRAME_SKIP == 0){
        //int i = memory.length;
        //int row=0;
        //int col=0;
        //System.out.println(""+memory.length);
        int time = (int) (System.currentTimeMillis() - Core.getInstance().start)>>3;

        //transformMemoryToImage();
        //buffer(Core._image,0,0);

        /*
        for(;--i >= 0;){
            if(i%Core.H==0){
             row++;
             col=0;
            }
        draw_Pixel(col,row,
            //System.out.println(""+memory[i]);
            //);
        col++;
        }*

        Core.graphics.setColor(0X00ffffff);

        //Core.graphics.drawString("start "+(time),0, 20,0);

        //Core.graphics.drawString("fps "+Core.getInstance().lastFps+" "+((time % 100)==0),0,0,0);

        if(time % 100 == 0){
                Core.graphics.setColor(0);
                Core.getInstance().lastFps = Core.getInstance().fpsCont;
                Core.getInstance().fpsCont = 0;
                //Core.start = 0;
           }


                Core.getInstance().flush();

                Core.getInstance().fpsCont++;

        }


        if ((Core.GLOBAL_TICKS % 20) == 0) {
            System.gc();
        }

        Core.getInstance().sync();//list = null;
    }

    public void bufferImage(int bitmap[]){
        System.out.println("start");
 int i = bitmap.length;
        for(int j=0;j<i;j++){
            memory[j]=bitmap[j];
        }
        System.out.println("end");

    }*/

    public void paint() {
        //if((Core.GLOBAL_TICKS % (Core.FRAME_SKIP)) == 0){
         Core.strategy.show();
         Core.graphics.dispose();
         //Core.graphics = null;
          Core.graphics = (Graphics2D) Core.strategy.getDrawGraphics();

          //Core.graphics.setColor(Color.black);
          //Core.graphics.fillRect(0, 0,Core.W,Core.H);
          
        //}
          
        if ((Core.GLOBAL_TICKS % (Core.FRAME_SKIP)) == 0) {
        System.gc();
        }
          Core.getInstance().sync();
        
    }

    /**
     * Get the rbg background color array;
     * @return
     */
    public int getRGB_Background() {
        return Core.color_background;
    }

   private void transformMemoryToImage(){
    //System.out.println("TRANSFORM");
    // BufferedImage img;
    // img.setRGB(i, i, i, i, memory, i, i)
   // Core._image = Image.createRGBImage(memory,Core.W,Core.H, true);
    
   }

}